package GameMain.View.PTGame
{
	import GameMain.Game;
	import GameMain.Model.Entity;
	import GameMain.Model.EntityMove;
	import GameMain.Model.MapSource;
	import GameMain.Model.Player;
	
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.MovieClip;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.utils.Dictionary;
	
	public class MapArea extends Sprite
	{
		private static var DEFAULT_P_X:int = 14;
		
		private var _bgMask:Sprite = new Sprite();
		
		private var _deepBgGroud:Sprite = new Sprite();
		private var _deepNpcGroud:Sprite = new Sprite();
		private var _deepPlayerGroud:Sprite = new Sprite;
		
		private var _mapSource:MapSource = null;
		
		private var _content1:Sprite = null;
		private var _content2:Sprite = null;
		private var _content3:Sprite = null;
		private var _content4:Sprite = null;
		
		private var _ratot2:Number = 0;
		private var _ratot3:Number = 0;
		
		
		public function MapArea()
		{
			super();
			
			this._bgMask.graphics.lineStyle(1);
			this._bgMask.graphics.beginFill(0xFFFFFF,1);
			this._bgMask.graphics.drawRect(0, 0, 965,475);
			this._bgMask.graphics.endFill();
			this.addChild(this._bgMask);
			this.mask = this._bgMask;
			
			this.addChild(this._deepBgGroud);
			this.addChild(this._deepNpcGroud);
			this.addChild(this._deepPlayerGroud);
			
			this.x = DEFAULT_P_X; this.y = 14;
			
		}
		
		public function setMap(mapS:MapSource):void
		{
			this._mapSource = mapS;
			
			this._content1 = new mapS.mapSourceClass1 as Sprite; //背景
			this._content1.width = this._bgMask.width;
			this._content1.height = this._bgMask.height;
			this._deepBgGroud.addChild(this._content1);
			
			var tempStep:Number = 500;
			
			this._content2 = new Sprite();
			var contnt:Sprite = new mapS.mapSourceClass2 as Sprite;
			contnt.x = - tempStep*2;
			this._content2.addChild(contnt);
			contnt = new mapS.mapSourceClass2 as Sprite;
			contnt.x = contnt.width - tempStep*2;
			this._content2.addChild(contnt);
			this._content2.y = Entity.MAP_STEP_Y;
			this._deepBgGroud.addChild(this._content2);
			
			this._content3 = new Sprite();
			contnt = new mapS.mapSourceClass3 as Sprite;
			contnt.x = - tempStep;
			this._content3.addChild(contnt);
			contnt = new mapS.mapSourceClass3 as Sprite;
			contnt.x = contnt.width - tempStep;
			this._content3.addChild(contnt);
			this._content3.y = Entity.MAP_STEP_Y;
			this._deepBgGroud.addChild(this._content3);
			
			this._content4 = new Sprite();
			contnt = new mapS.mapSourceClass4 as Sprite;
			contnt.x = - tempStep;
			this._content4.addChild(contnt);
			contnt = new mapS.mapSourceClass4 as Sprite; 
			contnt.x = contnt.width - tempStep;
			this._content4.addChild(contnt);
			contnt = new mapS.mapSourceClass4 as Sprite; 
			contnt.x = 2*contnt.width - tempStep;
			this._content4.addChild(contnt);
			contnt = new mapS.mapSourceClass4 as Sprite; 
			contnt.x = 3*contnt.width - tempStep;
			this._content4.addChild(contnt);
			this._content4.y = Entity.MAP_STEP_Y;
			this._deepBgGroud.addChild(this._content4);
			
			this._ratot2 = (this._content2.width - tempStep*4)/(this._content4.width - tempStep);
			this._ratot3 = (this._content3.width - tempStep)/(this._content4.width - tempStep);
		}
		
		public function addAllPlayer():void
		{
			var _player:Player;var i:Object;
			var dict:Dictionary = Game.EntityMgr.entityList
			for(i in dict)
			{
				_player = dict[i];
				_player.initModel();
				_player.addToParent(this.getPlayerGroud());
			}
			Game.ownPlayer.setCameraFun(onSetCamera);
			this.onSetCamera(Game.ownPlayer.x,0,Game.ownPlayer.face);
		}
		
		public function onSetCamera(_x:Number,_y:Number,_face:int):void
		{
			if(_face == EntityMove.FACE_RIGHT)
			{
				this.x = 200 - _x;
			}
			else
			{
				this.x = 700 - _x;
			}
			this._bgMask.x = DEFAULT_P_X - this.x
			this._content1.x = this._bgMask.x;
			this._content2.x = _x - this._ratot2 * _x;
			this._content3.x = _x - this._ratot3 * _x;
		}
		
		public function clearMap():void
		{
			while(this._deepBgGroud.numChildren)
			{
				this._deepBgGroud.removeChildAt(0);
			}
			while(this._deepNpcGroud.numChildren)
			{
				this._deepNpcGroud.removeChildAt(0);
			}
			while(this._deepPlayerGroud.numChildren)
			{
				this._deepPlayerGroud.removeChildAt(0);
			}
			this._content1 = null;
			this._content2 = null;
			this._content3 = null;
			this._content4 = null;
		}
		
		
		public function getBgGroud():Sprite
		{
			return this._deepBgGroud;
		}
		
		public function getNpcGroud():Sprite
		{
			return this._deepNpcGroud;
		}
		
		public function getPlayerGroud():Sprite
		{
			return this._deepPlayerGroud;
		}
		
	}
}